package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.AllMachine;
import com.baomidou.ant.entity.MachineInfo;
import com.baomidou.ant.entity.SysDept;
import com.baomidou.ant.entity.SysUser;
import com.baomidou.ant.mapper.AllMachineMapper;
import com.baomidou.ant.service.IAllMachineService;
import com.baomidou.ant.service.IMachineInfoService;
import com.baomidou.ant.service.ISysDeptService;
import com.baomidou.ant.service.ISysUserService;
import com.baomidou.ant.vo.request.RequestDtoVo;
import com.baomidou.ant.vo.request.RequestListMachineVo;
import com.baomidou.ant.vo.response.ResponseListMachineVo;
import com.baomidou.ant.vo.response.ResponseMachineNameVo;
import com.baomidou.ant.vo.response.ResponseMachineNumVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.xmlbeans.impl.xb.xsdschema.All;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class AllMachineServiceImpl extends ServiceImpl<AllMachineMapper, AllMachine> implements IAllMachineService {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IMachineInfoService machineInfoService;
    @Autowired
    private ISysDeptService sysDeptService;

    @Override
    public List<ResponseMachineNameVo> getMachine(RequestDtoVo requestDtoVo) {
        List<ResponseMachineNameVo> machineNameList=new ArrayList<>();
        QueryWrapper<MachineInfo> queryWrapper=new QueryWrapper<>();
        SysDept sysDept=sysDeptService.getById(requestDtoVo.getDeptId());
        queryWrapper.eq("dept_id",sysDept.getId());
        List<MachineInfo> machineInfoList=machineInfoService.list(queryWrapper);
        for(MachineInfo machineInfo:machineInfoList){
            ResponseMachineNameVo vo=new ResponseMachineNameVo();
            vo.setId(machineInfo.getId());
            vo.setMachineModel(machineInfo.getMachineModel());
            machineNameList.add(vo);
        }
//        }
        return machineNameList;
    }

    @Override
    public IPage<ResponseListMachineVo> listMachine(RequestListMachineVo requestListMachineVo) {
        IPage<AllMachine> machineInfoIPage=new Page<>(requestListMachineVo.getDisplayStart(),requestListMachineVo.getDisplayLength());
        QueryWrapper<AllMachine> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(requestListMachineVo.getMachineModel())){
            queryWrapper.eq("machine_info_name",requestListMachineVo.getMachineModel());
        }
        if(StringUtils.isNotBlank(requestListMachineVo.getMachineNum())){
            queryWrapper.like("machine_num",requestListMachineVo.getMachineNum());
        }
        if(ObjectUtils.isNotEmpty(requestListMachineVo.getStatus())){
            queryWrapper.eq("status",requestListMachineVo.getStatus());
        }
        SysUser user=userService.getById(requestListMachineVo.getUserId());
        //判断是否为管理员
        if(Objects.equals(user.getType(), "1")){
            //管理员条件查询
            if(StringUtils.isNotBlank(requestListMachineVo.getDeptName())){
                Integer deptId=sysDeptService.queryByDeptName(requestListMachineVo.getDeptName());
                queryWrapper.eq("dept_id",deptId);
            }
        }
        //不是管理员,只能看到本部门的
        else{
            queryWrapper.eq("dept_id",user.getDeptId());
        }
        IPage<AllMachine> page= page(machineInfoIPage,queryWrapper);
        return page.convert(allMachine -> {
            ResponseListMachineVo vo=new ResponseListMachineVo();
            BeanUtils.copyProperties(allMachine,vo);
            MachineInfo machineInfo=machineInfoService.getById(allMachine.getMachineInfoId());
            SysDept dept=sysDeptService.getById(machineInfo.getDeptId());
            vo.setDeptName(dept.getDept());
            vo.setMachineModel(machineInfo.getMachineModel());
            return vo;
        });

    }

    @Override
    public boolean queryByNum(String machineNum, Integer machineModel) {
        QueryWrapper<AllMachine> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("machine_num",machineNum);
        queryWrapper.eq("machine_info_id",machineModel);
        return getOne(queryWrapper)!=null;
    }

    @Override
    public List<ResponseMachineNumVo> getMachineNum(RequestDtoVo requestDtoVo) {
        List<ResponseMachineNumVo> machineNumList=new ArrayList<>();
        QueryWrapper<AllMachine> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dept_id",requestDtoVo.getDeptId());
        List<AllMachine> machineList=list(queryWrapper);
        for(AllMachine machine:machineList){
            ResponseMachineNumVo vo=new ResponseMachineNumVo();
            BeanUtils.copyProperties(machine,vo);
            machineNumList.add(vo);
        }
        return machineNumList;
    }
}

